package com.ruoyi.Courses.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.Courses.mapper.CoursesMapper;
import com.ruoyi.DegreePoints.mapper.DegreePointsMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Courses.domain.Courses;
import com.ruoyi.Courses.service.ICoursesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程信息Controller
 * 
 * @author ruoyi
 * @date 2025-04-17
 */
@RestController
@RequestMapping("/Courses/courses")
public class CoursesController extends BaseController
{
    @Autowired
    private ICoursesService coursesService;

    @Autowired
    private CoursesMapper coursesMapper;

    @Autowired
    private DegreePointsMapper degreePointsMapper;

    /**
     * 查询课程信息列表
     */
//    @PreAuthorize("@ss.hasPermi('Courses:courses:list')")
    @GetMapping("/list")
    public TableDataInfo list(Courses courses)
    {
        startPage();
        courses.setPointId(getLoginUser().getUserId());
        List<Courses> list = coursesService.selectCoursesList(courses,getLoginUser().getUsername().equals("admin")? 0 : getLoginUser().getUserId());
//        if(list.size() != 0 && getLoginUser().getUser().getRemark().equals("管理员")){
//            for (int i = 0; i < list.size(); i++) {
//                list.get(i).setPointName(degreePointsMapper.selectDegreePointsByPointId(list.get(i).getPointId()).getPointName());
//
//            }
//        }
        return getDataTable(list);
    }

    //查询专家审核课程
    @GetMapping("/byExpert")
    public TableDataInfo listByExpert(Courses courses)
    {
        startPage();
        courses.setPointId(getLoginUser().getUserId());
        List<Courses> list = coursesMapper.selectCoursesByExpert(getLoginUser().getUserId());
        if(list.size() != 0){
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setPointName(degreePointsMapper.selectDegreePointsByPointId(list.get(i).getPointId()).getPointName());

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('Courses:courses:export')")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Courses courses)
    {
        List<Courses> list = coursesService.selectCoursesList(courses, getLoginUser().getUserId());
        ExcelUtil<Courses> util = new ExcelUtil<Courses>(Courses.class);
        util.exportExcel(response, list, "课程信息数据");
    }

    /**
     * 获取课程信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('Courses:courses:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        return success(coursesService.selectCoursesByCourseId(courseId));
    }

    /**
     * 新增课程信息
     */
    @PreAuthorize("@ss.hasPermi('Courses:courses:add')")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Courses courses)
    {
        courses.setuId(getLoginUser().getUserId());
        courses.setPointId(getLoginUser().getUserId());
        return toAjax(coursesService.insertCourses(courses));
    }

    /**
     * 修改课程信息
     */
    @PreAuthorize("@ss.hasPermi('Courses:courses:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Courses courses)
    {
        return toAjax(coursesService.updateCourses(courses));
    }

    /**
     * 删除课程信息
     */
    @PreAuthorize("@ss.hasPermi('Courses:courses:remove')")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        return toAjax(coursesService.deleteCoursesByCourseIds(courseIds));
    }
}
